<script>
import { routes } from "@/router/index";
import {
    PictureFilled,
    Goods,
    Folder,
    DataLine,
    Edit,
    Location,
} from '@element-plus/icons-vue'
import { mapState } from 'vuex'
export default {
    name: 'AsideCom',
    props: ['collapse'],
    data() {
        return {
            routes: routes[0].children,
            path: ''
        }
    },
    components: {
        PictureFilled,
        Goods,
        Folder,
        DataLine,
        Edit,
        Location,
    },
    computed: {
        ...mapState(['userInfo']),
        menus() {
            // 如果当前登录的是 admin 那么我们需要开放所有权限，否则开发自身权限
            return this.userInfo.adminname == 'admin' ? routes[0].children : this.userInfo.checkedkeys
        }
    },
    mounted() {
        // console.log(this.$store.state.count);
        // ElMessage.info('显示：' + this.$store.state.count)

        // $router: 用来做页面跳转用的
        // $route: 用来获取路由信息用的
        console.log(this.$route);
    },
    watch: {
        $route: {
            // 强制立即执行回调
            immediate: true,
            handler(newRoute) {
                console.log(newRoute.path);
                // 要记录当前页面的路径
                this.path = newRoute.path
            }
        }
    }
}
/*
    获取数据：
    1. 在 js 中使用 this.$store.state.count  获取数据
    2. 在 页面模板中通过 {{ $store.state.count }} 获取数据
    3. 在 js 中使用 mapState 的方式获取
*/
</script>
<template>
    <!-- router 可以进行路由跳转，跳转的路径为  index 属性的值  -->
    <el-menu unique-opened :default-active="path" router :collapse-transition="false" :collapse="collapse"
        default-active="2" class="el-menu-vertical-demo">

        <el-menu-item index="/">
            <el-icon>
                <HomeFilled />
            </el-icon>
            <span>首页 {{ $store.state.count }} </span>
        </el-menu-item>



        <el-sub-menu v-for="item in menus" :key="item.path" :index="item.path">
            <template #title>
                <el-icon>
                    <component :is="item.icon" />
                </el-icon>
                <span>{{ item.label }}</span>
            </template>
            <el-menu-item-group>
                <el-menu-item v-for="child in item.children" :key="child.path" :index="`/${item.path}/${child.path}`">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>{{ child.label }}</span>
                    </template>
                </el-menu-item>
            </el-menu-item-group>
        </el-sub-menu>


        <!-- <el-sub-menu index="1">
            <template #title>
                <el-icon>
                    <UserFilled />
                </el-icon>
                <span>用户管理</span>
            </template>
            <el-menu-item-group>
                <el-menu-item index="1-1">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>用户列表</span>
                    </template>
                </el-menu-item>
                <el-menu-item index="1-2">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>管理员列表</span>
                    </template>
                </el-menu-item>
            </el-menu-item-group>
        </el-sub-menu> -->
    </el-menu>
</template>